package com.sghd.candy.module.account.service;

import com.sghd.candy.module.account.manager.Account;
import com.sghd.candy.module.account.manager.AccountManager;
import com.sghd.candy.module.player.manager.Player;
import com.sghd.pojo.candy.account.AccountState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author frank
 * @date 2018-10-17 10:00
 * @desc
 **/
@Component
public class AccountServiceimpl implements AccountService {
    @Autowired
    private AccountManager accountManager;

    @Override
    public Account load(long id) {
        return accountManager.load(id);
    }

    @Override
    public Account create(String uid) {
        return accountManager.create(uid);
    }

    @Override
    public Account loadByUid(String uid) {
        Account account = accountManager.find(uid);
        if (account == null) {
            //查找数据库
            account = accountManager.loadByUid(uid);
            if (account != null) {
                //数据加载到缓存中
                account = load(account.getId());
            }
        }
        return account;
    }

    @Override
    public void login(Account account, String ip, boolean guest, int serverId) {
        accountManager.login(account, ip, guest, serverId);
    }

    /**
     * 封号
     * @param playerId
     */
    public void block(long playerId) {
        Account account = accountManager.load(playerId);
        if (account == null) {
            return;
        }
        accountManager.addState(account, AccountState.BLOCK);
    }

    /**
     * 解封
     * @param playerId
     */
    public void deblock(long playerId) {
        Account account = accountManager.load(playerId);
        if (account == null) {
            return;
        }
        accountManager.removeState(account, AccountState.BLOCK);
    }

    @Override
    public void upgrade(Account account) {
        account.upgrade();
    }

    @Override
    public void logout(Account account, Player player) {
        accountManager.logout(account);
    }

}
